package com.example.administrator.qimo3no74.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.administrator.qimo3no74.R;
import com.example.administrator.qimo3no74.home.ButtonIcon;
import com.example.administrator.qimo3no74.home.ButtonIconAdapter;
import com.example.administrator.qimo3no74.home.Commodity;
import com.example.administrator.qimo3no74.home.CommodityAdapter;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends Fragment implements OnBannerListener {

    private Banner mBanner;
    private MyImageLoader mMyImageLoader;
    private ArrayList<Integer> imagePath;
    private ArrayList<String> imageTitle;
    private List<ButtonIcon> buttonIcons = new ArrayList<>();
    private List<Commodity> commodities = new ArrayList<>();

    public HomeFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        //轮播图
        initData();
        initView(view);
        //广告图片
        ImageView homeImageView1 = view.findViewById(R.id.homeImageView1);
        homeImageView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(), "你点了第一张广告图", Toast.LENGTH_SHORT).show();

            }
        });

        //活动图片
        ImageView homeImageView2 = view.findViewById(R.id.homeImageView2);
        homeImageView2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(), "你点了第一张活动图", Toast.LENGTH_SHORT).show();
            }


        });

        //按钮小图标
        initButtonIcon();
        actionButtonIcon(view);

        //商品列表
        initCommodity();
        actionCommodity(view);

        return view;
    }

    private void actionCommodity(View view) {
        RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.commoditiesRecyclerView);
        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        CommodityAdapter adapter = new CommodityAdapter(commodities);
        recyclerView.setAdapter(adapter);
    }

    private void initCommodity() {
        commodities.add(new Commodity("毛呢背心裙秋冬女2018新款修身显瘦中长款背带无袖打底马甲A字裙",R.drawable.beixin_qun1,129.00,"58997人已付款"));
        commodities.add(new Commodity("欧美高档简约时尚H型睡衣风雪纺纱连衣裙女2019年春季新款",R.drawable.lianyi_qun,118.00,"1174人已付款"));
        commodities.add(new Commodity("游戏耳机入耳式吃鸡带麦重低音7.1电脑头戴式电竞耳麦台式笔记本手机通用绝地求生刺激战场竞技耳塞",R.drawable.er_ji,89.00,"5995人已付款"));
        commodities.add(new Commodity("欧洲站流行女2018新款时尚网红高端大牌东大门x2羽绒服中长款过膝",R.drawable.yurong_fu_nv,638.00,"256人已付款"));
        commodities.add(new Commodity("Meizu/魅族 Note8 国民拍照全面屏手机 6英寸大屏 骁龙632八核处理器 后置旗舰双摄",R.drawable.meizu_note8,1298.00,"28067人已付款"));
        commodities.add(new Commodity("花花公子男士羽绒服2018新款冬装冬季加厚青年帅气休闲连帽外套潮",R.drawable.yurong_fu,196.8,"25870人已付款"));
        commodities.add(new Commodity("花花公子男士羽绒服2018新款冬装冬季加厚青年帅气休闲连帽外套潮",R.drawable.yurong_fu,196.8,"25870人已付款"));
        commodities.add(new Commodity("花花公子男士羽绒服2018新款冬装冬季加厚青年帅气休闲连帽外套潮",R.drawable.yurong_fu,196.8,"25870人已付款"));
        commodities.add(new Commodity("花花公子男士羽绒服2018新款冬装冬季加厚青年帅气休闲连帽外套潮",R.drawable.yurong_fu,196.8,"25870人已付款"));
        commodities.add(new Commodity("花花公子男士羽绒服2018新款冬装冬季加厚青年帅气休闲连帽外套潮",R.drawable.yurong_fu,196.8,"25870人已付款"));
    }


    private void actionButtonIcon(View view) {
        RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.buttonIconRecyclerView);
        GridLayoutManager layoutManager = new GridLayoutManager(getActivity(),6);
        recyclerView.setLayoutManager(layoutManager);
        ButtonIconAdapter adapter = new ButtonIconAdapter(buttonIcons);
        recyclerView.setAdapter(adapter);
    }

    private void initButtonIcon() {

        buttonIcons.add(new ButtonIcon("微笑",R.drawable.home_1));
        buttonIcons.add(new ButtonIcon("大笑",R.drawable.home_2));
        buttonIcons.add(new ButtonIcon("调皮",R.drawable.home_3));
        buttonIcons.add(new ButtonIcon("可爱",R.drawable.home_4));
        buttonIcons.add(new ButtonIcon("闭眼笑",R.drawable.home_5));
        buttonIcons.add(new ButtonIcon("笑哭",R.drawable.home_6));
        buttonIcons.add(new ButtonIcon("喜爱",R.drawable.home_7));
        buttonIcons.add(new ButtonIcon("呆呆",R.drawable.home_8));
        buttonIcons.add(new ButtonIcon("痛苦",R.drawable.home_9));
        buttonIcons.add(new ButtonIcon("汗颜",R.drawable.home_10));
        buttonIcons.add(new ButtonIcon("难过",R.drawable.home_11));
        buttonIcons.add(new ButtonIcon("惊吓",R.drawable.home_12));
    }

    private void initData() {
        imagePath = new ArrayList<>();
        imageTitle = new ArrayList<>();
        imagePath.add(R.drawable.home_gao1);
        imagePath.add(R.drawable.home_gao2);
        imagePath.add(R.drawable.home_gao3);
        imagePath.add(R.drawable.home_gao4);
        imageTitle.add("我是一号");
        imageTitle.add("我是二号");
        imageTitle.add("我是三号");
        imageTitle.add("我是四号");
    }

    private void initView(View view) {
        mMyImageLoader = new MyImageLoader();
        mBanner = view.findViewById(R.id.banner);
        //设置样式，里面有很多种样式可以自己都看看效果
        mBanner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        //mBanner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE);
        //设置图片加载器
        mBanner.setImageLoader(mMyImageLoader);
        //设置轮播的动画效果,里面有很多种特效,可以都看看效果。
        mBanner.setBannerAnimation(Transformer.Default);
        //mBanner.setBannerAnimation(Transformer.ZoomOutSlide);
        //轮播图片的文字
        mBanner.setBannerTitles(imageTitle);
        //设置轮播间隔时间
        mBanner.setDelayTime(3000);
        //设置是否为自动轮播，默认是true
        mBanner.isAutoPlay(true);
        //设置指示器的位置，小点点，居中显示
        mBanner.setIndicatorGravity(BannerConfig.CENTER);
        //设置图片加载地址
        mBanner.setImages(imagePath)
                //轮播图的监听
                .setOnBannerListener(this)
                //开始调用的方法，启动轮播图。
                .start();

    }


    @Override
    public void OnBannerClick(int position) {
        Toast.makeText(getActivity(), "你点了第" + (position + 1) + "张轮播图", Toast.LENGTH_SHORT).show();
    }


    /**
     * 图片加载类
     */
    private class MyImageLoader extends ImageLoader {
        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            Glide.with(context.getApplicationContext())
                    .load(path)
                    .into(imageView);
        }
    }





}
